
package com.be.beadmin.material.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.material.dto.input.MaterialCodeInfoDto;
import com.be.beadmin.material.dto.output.MaterialCodeInfoEx;
import com.be.beadmin.material.entity.MaterialCodeInfo;
import com.be.beadmin.material.entity.MaterialCodeInfo_;
import com.be.beadmin.material.mapper.MaterialCodeInfoMapper;
import com.be.beadmin.material.mapper.MaterialCodeInfoMapperEx;
import com.be.beadmin.material.repository.MaterialCodeInfoRepository;
import com.be.beadmin.material.service.MaterialCodeInfoService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zmy
* @date 2023-06-11
**/
@Service
@RequiredArgsConstructor
public class MaterialCodeInfoServiceImpl implements MaterialCodeInfoService {

    @Autowired
    private MaterialCodeInfoRepository materialCodeInfoRepository;
    @Autowired
    private MaterialCodeInfoMapper materialCodeInfoMapper;
    @Autowired
    private MaterialCodeInfoMapperEx materialCodeInfoExMapper;

    @Override
    public Page<MaterialCodeInfoEx> getMaterialCodeInfoByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<MaterialCodeInfo> page = materialCodeInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(MaterialCodeInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(MaterialCodeInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<MaterialCodeInfoEx> page1 = page.map(materialCodeInfoExMapper::toEntity);
            return page1;

    }

    @Override
    public void addMaterialCodeInfo(MaterialCodeInfoDto materialCodeInfoDto) {
            MaterialCodeInfo result = materialCodeInfoRepository.findMaterialCodeInfoByIdAndDelete(materialCodeInfoDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("MaterialCodeInfo不存在,请重新查询");
            }
            MaterialCodeInfo materialCodeInfo = materialCodeInfoMapper.toEntity(materialCodeInfoDto);
            materialCodeInfoRepository.save(materialCodeInfo);
    }

    @Override
    public void updateMaterialCodeInfo(MaterialCodeInfoDto materialCodeInfoDto) {
            MaterialCodeInfo materialCodeInfo = materialCodeInfoRepository.findMaterialCodeInfoById(materialCodeInfoDto.getId());
            materialCodeInfo = materialCodeInfoMapper.toEntity(materialCodeInfoDto);
            materialCodeInfoRepository.save(materialCodeInfo);
    }


    @Override
    public void delMaterialCodeInfo(String id) {
        MaterialCodeInfo materialCodeInfo = materialCodeInfoRepository.findMaterialCodeInfoById(id);
        materialCodeInfo.setDelete(1);
        materialCodeInfoRepository.save(materialCodeInfo);
    }

    @Override
    public void delMaterialCodeInfos (List<String> ids) { }
    @Override
    public void addInfo(MultipartFile[] files) throws IOException {
        for(MultipartFile file:files) {
            InputStream inputStream = file.getInputStream();
            Workbook workbook = new HSSFWorkbook(inputStream);
            for (int i = 0; i < 4; i++) {
                Sheet sheet = workbook.getSheetAt(i);
                for (Row row : sheet) {
                    // 跳过表头
                    if (row.getRowNum() == 0) {
                        continue;
                    }
                    MaterialCodeInfo materialCodeInfo = new MaterialCodeInfo();
                    if (row.getCell(0) != null)
                        materialCodeInfo.setMaterialCode(row.getCell(0).getStringCellValue());

                    if (row.getCell(1) != null)
                        materialCodeInfo.setMaterialName(row.getCell(1).getStringCellValue());

                    if (row.getCell(3) != null)
                        materialCodeInfo.setUnit(String.valueOf(row.getCell(3).getStringCellValue()));

                    if (row.getCell(4) != null)
                        materialCodeInfo.setMaterialSpec(row.getCell(4).getStringCellValue());

                    materialCodeInfoRepository.save(materialCodeInfo);

                }

            }
        }
    }

    @Override
    public String getMaterialNameByMaterialCode(String materialCode) {
        MaterialCodeInfo materialCodeInfo = materialCodeInfoRepository.findMaterialCodeInfoByMaterialCodeAndDelete(materialCode , 0);
        String materialName = materialCodeInfo.getMaterialName();
        return materialName;
    }
}